package rentadomus

import org.springframework.dao.DataIntegrityViolationException

class PqrController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        if (session.person) {
            redirect(action: "list", params: params)
        }else{
            redirect(action:"login")
        }
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        def myPqrs=[]
        def otherPqr=[]
        Place.findAllByIdPerson(session.person).each(){
            Pqr.findAllByPlace(it).each(){pqr->
                otherPqr.add pqr
            }
        }
        for(Contract contract:Contract.list()){
            if(contract.person.id==session.person.id){
                Pqr.findAllByPlace(contract.place).each(){
                    myPqrs.add it
                }
            }
        }
//        [pqrInstanceList: Pqr.list(params), pqrInstanceTotal: Pqr.count(),otherPqr:otherPqr,otherPqrTotal:otherPqr.size()]
        [pqrInstanceList: myPqrs, pqrInstanceTotal: Pqr.count(),otherPqr:otherPqr,otherPqrTotal:otherPqr.size()]
    }

    def create() {
        def places=[]
        for(Contract contract:Contract.list()){
            if(contract.person.id==session.person.id){
                places.add contract.place
            }
        }
        [pqrInstance: new Pqr(params),places:places]
    }

    def save() {
        def pqrInstance = new Pqr(params)
        pqrInstance.setStatus("Requested")
        pqrInstance.setDateRequested(new Date())
        if (!pqrInstance.save(flush: true)) {
            render(view: "create", model: [pqrInstance: pqrInstance])
            return
        }

//        flash.message = message(code: 'default.created.message', args: [message(code: 'pqr.label', default: 'Pqr'), pqrInstance.id])
        flash.message = "Your request has been sent successfully!"
//        redirect(action: "show", id: pqrInstance.id)
        redirect(action: "list")
    }

    def show(Long id) {
        def pqrInstance = Pqr.get(id)
        def visible=false
        def display=pqrInstance.getStatus().equals("Resolved")?"none":"block"
        if (!pqrInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'pqr.label', default: 'Pqr'), id])
            redirect(action: "list")
            return
        }
        for(Contract contract:Contract.list()){
            if(contract.person.id==session.person.id && contract.place==pqrInstance.place){
                visible=true
            }
        }
        [pqrInstance: pqrInstance,visible: visible,display:display]
    }

    def edit(Long id) {
        def places=[]
        for(Contract contract:Contract.list()){
            if(contract.person.id==session.person.id){
                places.add contract.place
            }
        }
        def pqrInstance = Pqr.get(id)
        if (!pqrInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'pqr.label', default: 'Pqr'), id])
            redirect(action: "list")
            return
        }

        [pqrInstance: pqrInstance,places:places]
    }

    def update(Long id, Long version) {
        def pqrInstance = Pqr.get(id)
        if (!pqrInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'pqr.label', default: 'Pqr'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (pqrInstance.version > version) {
                pqrInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'pqr.label', default: 'Pqr')] as Object[],
                          "Another user has updated this Pqr while you were editing")
                render(view: "edit", model: [pqrInstance: pqrInstance])
                return
            }
        }

        pqrInstance.properties = params

        if (!pqrInstance.save(flush: true)) {
            render(view: "edit", model: [pqrInstance: pqrInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'pqr.label', default: 'Pqr'), pqrInstance.id])
        redirect(action: "show", id: pqrInstance.id)
    }

    def delete(Long id) {
        def pqrInstance = Pqr.get(id)
        if (!pqrInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'pqr.label', default: 'Pqr'), id])
            redirect(action: "list")
            return
        }

        try {
            pqrInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'pqr.label', default: 'Pqr'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'pqr.label', default: 'Pqr'), id])
            redirect(action: "show", id: id)
        }
    }
    
    def resolve(Long id){
        Pqr pqrInstance = Pqr.get(id)
        pqrInstance.setDateResolved(new Date())
        pqrInstance.setStatus("Resolved")
        pqrInstance.save(flush:true)
        redirect(action:"list")
    }
}
